package com.example.spring_boot_demo.controller;

import com.example.spring_boot_demo.dto.ClazzRoom;
import com.example.spring_boot_demo.dto.User;
import com.example.spring_boot_demo.utils.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/5/28:23:07
 */

@SuppressWarnings({"unchecked", ""})
@Controller
@Validated
@RequestMapping("/form")
public class DemoController {
    private static final Logger log = LoggerFactory.getLogger(DemoController.class);

    @RequestMapping("/findOne")
    @ResponseBody
    public String findOne(){

        return "this is response";
    }
    /**
     *  接收字符串传参！
     * @param name
     */
    //@RequestMapping("/StringValue")
    //@ResponseBody
    //public String receiveParameters(String name){
    //    System.out.println(name);
    //    return name;
    //}



    /**
     * 前端将name 改为 username
    */
    @RequestMapping("/StringValue")
    @ResponseBody
    public String receiveParameters(@RequestParam("username") String name){
        return name;
    }

    /**
     *接收前端form提交的数组
     * @param ids
     */
    @RequestMapping("/Arrays")
    @ResponseBody
    public Map receiveParameters(int[] ids){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("ids",ids);
        hashMap.put("code",200);
        return hashMap;
    }


    /**
     * 传入一个数组，后端自动封装成List集合
     * @param ids
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map receiveParameters(@RequestParam List<Integer> ids){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("ids",ids);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 传入一堆数据，后端自动封装成Map集合
     * @param user
     */
    @ResponseBody
    @RequestMapping("/map")
    public Map receiveParameters(@RequestParam Map<String,Object> user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     *  传入一堆数据，后端自动封装成User对象
     * @param user
     */
    @ResponseBody
    @RequestMapping("/user")
    public Map receiveParameters(User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 复合对象数据传输
     * @param clazzRoom
     */
    @RequestMapping("/ClazzUserValue")
    @ResponseBody
    public Map<Object, Object> receiveParameters(ClazzRoom clazzRoom){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",clazzRoom);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 对于时间参数，使用json
     * @param user
     */
    @ResponseBody
    @RequestMapping("/date")
    public Map receiveParametersByDate( User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 参数校验
     * @param user
     */
    @ResponseBody
    @RequestMapping("/checkParam")
    public Map checkParam(
            @Validated User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }
    /**
     * 参数校验
     * @param name
     */
    @ResponseBody
    @RequestMapping("/findStudentById")
    public AjaxResult findStudentById(@NotBlank(message = "name 不能为空") String name){
        User user = new User();
        //user.setAge(10);
        //user.setName("123");
        return AjaxResult.success(user);
    }

}
